package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Order;
import com.itheima.domain.OrderItem;
import com.itheima.mapper.OrderItemMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.service.IOrderItemService;
import com.itheima.util.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单中所包含的商品 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements IOrderItemService {


    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    //根据订单id查询指定订单详情
    public Order findById(Long id) {
        return orderMapper.selectById(id);
    }

    //根据订单id集合查询订单详情
    public List<Order> findByIds(List<Long> ids) {
        return orderMapper.selectBatchIds(ids);
    }

    //订单分页查询和条件查询
    @Override
    public Page findOrderList(Integer pageNum, Integer pageSize, Order order) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<Order> qw = new QueryWrapper<>();
//        if (StringUtils.isNotEmpty(order.getCreateTime().toString())) {
        if (order.getCreateTime() != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            //                date = format.parse(order.getCreateTime().toString());
            date = Date.from(order.getCreateTime().atZone( ZoneId.systemDefault()).toInstant());
            String startTime = DateUtils.getStartOfDay(date);
            String endTime = DateUtils.getEndOfDay(date);
            qw.between("create_time", startTime, endTime);
        }
        qw.eq(StringUtils.isNotEmpty(order.getOrderSn()), "order_sn", order.getOrderSn()) //订单编号
                .eq(order.getOrderType() != null, "order_type", order.getOrderType())  //订单类型
                .eq(order.getSourceType() != null, "source_type", order.getSourceType())  //订单来源
                .eq("delete_status", 0)  //删除状态：0->未删除
                .eq(order.getStatus() != null, "status", order.getStatus())//订单状态
                .like(StringUtils.isNotEmpty(order.getReceiverName()), "receiver_name", order.getReceiverName())//收货人
                .eq(StringUtils.isNotEmpty(order.getReceiverPhone()), "receiver_phone", order.getReceiverPhone())//收货人手机号码
                .orderByDesc("create_time");
        orderMapper.selectPage(page, qw);
        return page;

    }

    //修改订单费用信息
    public Integer updateOrder(Order order) {
        return orderMapper.updateById(order);
    }

    //批量更新订单
    public Integer updateOrderList(List<Order> orderList) {
        Integer count = null;
        for (Order order : orderList) {
            count = orderMapper.updateById(order);
        }
        return count;
    }

    //根据订单id查询订单所包含的商品
    public List<OrderItem> findOrderItem(Long orderId) {
        QueryWrapper<OrderItem> qw = new QueryWrapper<>();
        qw.eq("order_id", orderId);
        return orderItemMapper.selectList(qw);
    }
}
